home *** CD-ROM | disk | FTP | other *** search
/ EuroCD 3 / EuroCD 3.iso / Programming / vbcc / mbasic.c < prev    next >
C/C++ Source or Header  |  1998-06-24  |  4KB  |  229 lines

  1. /*  Test-language for vbcc. */
  2.  
  3. #include "supp.h"
  4.  
  5. #include <ctype.h>
  6. #include <stdio.h>
  7.  
  8. struct Var *fv;
  9.  
  10. struct Typ tint,mfunc;
  11. struct struct_declaration msd; /* initialized to zero */
  12.  
  13. FILE *file;
  14. char *next;
  15.  
  16. struct obj expression(),factor(),scalar();
  17.  
  18. void raus(void)
  19. {
  20.     while(fv){
  21.         struct Var *m=fv->next;
  22.         free(fv);
  23.         fv=m;
  24.     }
  25.     while(first_ic){
  26.         struct IC *m=first_ic->next;
  27.         free(first_ic);
  28.         first_ic=m;
  29.     }
  30.     exit(0);
  31. }
  32. void add_IC(struct IC *new)
  33. {
  34.     new->next=0;
  35.     new->prev=last_ic;
  36.     new->change_cnt=new->use_cnt=0;
  37.     new->line=0;
  38.     new->file=0;
  39.     new->q1.am=new->q2.am=new->z.am=0;
  40.     if(!last_ic){
  41.         first_ic=new;
  42.     }else{
  43.         last_ic->next=new;
  44.     }
  45.     last_ic=new;
  46. }
  47. struct Var *add_var(char *name,struct Typ *t,int sc)
  48. {
  49.     struct Var *v=mymalloc(sizeof(*v));
  50.     v->vtyp=t;
  51.     v->storage_class=sc;
  52.     v->reg=0;
  53.     v->identifier=name;
  54.     v->offset=max_offset;
  55.     if(sc==AUTO) max_offset=zladd(max_offset,sizetab[t->flags&NQ]);
  56.     v->priority=1;
  57.     v->flags=0;
  58.     v->next=fv;
  59.     v->clist=0;
  60.     v->fi=0;
  61.     v->inline_copy=0;
  62.     v->nesting=1;
  63.     fv=v;
  64.     return v;
  65. }
  66. struct Var *add_tmp_var(struct Typ *t)
  67. {
  68.     return add_var(empty,t,AUTO);
  69. }
  70. struct Var *get_var(char *name)
  71. {
  72.     struct Var *v;char *buf;
  73.     puts("getvar");
  74.     for(v=fv;v;v=v->next){
  75.         if(!strcmp(name,v->identifier)) return v;
  76.     }
  77.     buf=mymalloc(strlen(name)+1);
  78.     strcpy(buf,name);
  79.     return add_var(buf,&tint,AUTO);
  80. }
  81.  
  82. char *identifier(void)
  83. {
  84.   static char id[1024];
  85.   char *s=id;
  86.   puts("identifier");
  87.   while(isalnum(*next)) *s++=*next++;
  88.   puts("done");
  89.   return id;
  90. }
  91. struct obj scalar(void)
  92. {
  93.   struct obj o;
  94.   zlong val;
  95.   puts("scalar");
  96.   if(isdigit(*next)){
  97.     o.flags=KONST;
  98.     val=l2zl(0L);
  99.     while(isdigit(*next)){
  100.       val=zlmult(val,l2zl(10L));
  101.       val=zladd(val,l2zl((long)(*next-'0')));
  102.       next++;
  103.     }
  104.     o.val.vint=zl2zi(val);
  105.     return o;
  106.   }
  107.   if(*next=='('){
  108.     next++;
  109.     o=expression();
  110.     next++;
  111.     return o;
  112.   }
  113.   o.flags=VAR;
  114.   o.val.vlong=l2zl(0L);
  115.   o.v=get_var(identifier());
  116.   return o;
  117. }
  118. struct obj factor(void)
  119. {
  120.   struct obj o;
  121.   struct IC *new;
  122.   puts("factor");
  123.   o=scalar();
  124.   while(*next=='*'||*next=='/'){
  125.     new=mymalloc(ICS);
  126.     if(*next=='*') new->code=MULT; else new->code=DIV;
  127.     next++;
  128.     new->typf=INT;
  129.     new->q1=o;
  130.     new->q2=scalar();
  131.     o.flags=VAR;
  132.     o.v=add_tmp_var(&tint);
  133.     o.val.vlong=l2zl(0L);
  134.     new->z=o;
  135.     add_IC(new);
  136.   }
  137.   return o;
  138. }
  139. struct obj expression(void)
  140. {
  141.   struct obj o;
  142.   struct IC *new;
  143.   puts("expression");
  144.   o=factor();
  145.   while(*next=='+'||*next=='-'){
  146.     new=mymalloc(ICS);
  147.     if(*next=='+') new->code=ADD; else new->code=SUB;
  148.     next++;
  149.     new->typf=INT;
  150.     new->q1=o;
  151.     new->q2=factor();
  152.     o.flags=VAR;
  153.     o.v=add_tmp_var(&tint);
  154.     o.val.vlong=l2zl(0L);
  155.     new->z=o;
  156.     add_IC(new);
  157.   }
  158.   return o;
  159. }
  160. void compile(void)
  161. {
  162.   struct IC *new;
  163.   char line[1024],*s;
  164.   struct obj o,last;
  165.   puts("compile");
  166.   while(fgets(line,1023,file)){
  167.     next=line;
  168.     s=identifier();
  169.     if(*next=='='){
  170.       struct Var *v=get_var(s);
  171.       next++;
  172.       o=expression();
  173.       new=mymalloc(ICS);
  174.       new->code=ASSIGN;
  175.       new->typf=INT;
  176.       new->q1=o;
  177.       new->z.flags=VAR;
  178.       new->z.v=v;
  179.       new->z.val.vlong=l2zl(0L);
  180.       new->q2.flags=0;
  181.       new->q2.val.vlong=sizetab[INT];
  182.       last=new->z;
  183.       add_IC(new);
  184.       continue;
  185.     }
  186.   }
  187.   new=mymalloc(ICS);
  188.   new->code=SETRETURN;
  189.   new->typf=INT;
  190.   new->q1=last;
  191.   new->q2.flags=new->z.flags=0;
  192.   new->q2.val.vlong=sizetab[INT];
  193.   new->z.reg=freturn(&tint);
  194.   if(!new->z.reg) puts("problem!");
  195.   add_IC(new);
  196. }
  197. void error(int n,...)
  198. {
  199.     printf("error %d\n",n);
  200.     raus();
  201. }
  202. void savescratch()
  203. {}
  204.  
  205. main(int argc,char **argv)
  206. {
  207.     struct Var *main;
  208.     max_offset=l2zl(0L);
  209.     if(!init_cg()) raus();
  210.     tint.flags=INT;
  211.     tint.next=0;
  212.     mfunc.flags=FUNKT;
  213.     mfunc.next=∭
  214.     mfunc.exact=&msd;
  215.     main=add_var("main",&mfunc,EXTERN);
  216.     file=fopen(argv[1],"r");
  217.     if(!file) {printf("Error opening file\n");raus();}
  218.     compile();
  219.     scanf("%ld",&optflags);
  220.     pric(stdout,first_ic);
  221.     vl1=vl3=0;
  222.     vl2=fv;
  223.     optimize(optflags,main);
  224.     pric(stdout,first_ic);
  225.     gen_code(stdout,first_ic,main,max_offset);
  226.     raus();
  227. }
  228.  
  229.